<?php
defined('IN_TS') or die('Access Denied.');

$userid = aac('user')->isLogin();

//判断发布者状态
if(aac('user')->isPublisher()==false) tsNotice('不好意思，你还没有权限发布内容！');

switch($ts){

	case "":
		$arrOptions = $new['gift']->findAll('gift_options');
		foreach($arrOptions as $item){
			$strOption[$item['optionname']] = stripslashes($item['optionvalue']);
		}

		if ($strOption['allowpost'] == 0 && $TS_USER['isadmin'] == 0) {
//        if ($TS_APP['allowpost'] == 0 && $TS_USER['isadmin'] == 0) {
            tsNotice('系统设置不允许会员发商品！');
        }

		$strTopic = aac('group')->find('group_topic',array(
			'topicid'=>250,
		));
		$strTopic['content'] = tsDecode($strTopic['content'],$tp);

		$title = '发布商品';
		include template("add");
	
		break;
	
	case "do":
	
		$title = trim($_POST['title']);
		$cate = intval($_POST['cate']);
		$typeid = intval($_POST['typeid']);
		$limittime = trim($_POST['limittime']);
		$province = trim($_POST['province']);
		$city = trim($_POST['city']);
		$address = trim($_POST['address']);
		$transfer = trim($_POST['transfer']);
		$content = tsClean($_POST['content']);
		$price = trim($_POST['price']);
		$quota = trim($_POST['quota']);
		$photo = trim($_POST['photo']);

		// $img_path = getTextPhotos(tsDecode($content));
		if($photo==''){
			tsNotice('必须上传封面照片！');
		};
		// $count = count($img_path);
		$img_path = $photo;
		//$url=preg_replace('#/$#','',SITE_URL);
		//$img_path = $url.$img_path;

		$limittime = trim(str_replace('AM','',$limittime));
		$limittime = trim(str_replace('PM','',$limittime));
		$limittime = strtotime($limittime);

		if($limittime==''){
			tsNotice('设一个期限呗');
		}

		if($quota==''){
			tsNotice('数目不能为空');
		}

		if($title == '' || $content == ''){
			tsNotice('标题和简介不能为空');
		}

		if($province == '' || $city == '' ){
			tsNotice('省和市必须填写');
		}

        //1审核后显示0不审核
        if ($TS_APP['isaudit'] == 1) {
            $isaudit = 1;
        } else {
            $isaudit = 0;
        }


		
		$giftid = $new['gift']->create('gift',array(
			
			'userid'	=> $userid,
			'title'	=> $title,
			'typeid' => $typeid,
			'cate' => $cate,
			'limittime' => $limittime,
			'content' => $content,
            'price'=>$price,
			'isaudit'=>$isaudit,
			'addtime'	=> time(),
			'quota'=>$quota,
			'province'	=> $province,
			'city'  => $city,
			'address'	=> $address,
			'transfer'=>$transfer,
			'path'=> dirname($img_path),
			'photo'=> $img_path,
			'status' => 0,
			'face' => '',
			'coordinate' => '',
			'count_recommend' => 0,
		));

		//上传
		// $arrUpload = tsUploadPhotoUrl($img_path,$giftid,'gift',array('jpg','gif','png'));
		// if($arrUpload){
		// 	$new['gift']->update('gift',array(
		// 		'giftid'=>$giftid,
		// 	),array(
		// 		'path'=>$arrUpload['path'],
		// 		'photo'=>$arrUpload['url'],
		// 	));
		// }

		$url=tsUrl('gift','show',array('id'=>$giftid));
		$path = 'uploadfile/gift/qrcode/';
		/*
		$url = tsUrl('gift','notice',array('id'=>$giftid));
		$path = 'uploadfile/gift/notice/';
		$pdf = html2pdf($url, $path, 414 ,736);
		$img = pdf2png($pdf,$path.$giftid.'.png',414,36);
*/

		//生成二维码
		makeqrcode($url,$path,$giftid);
        //发送消息
		$msg_userid = '0';
		$msg_touserid = $userid;
		$msg_content = '你发布的商品《'.$title.'》海报已经生成，请点击链接访问，可以下载图片转发到你的朋友圈，鼓动你的朋友下单哦。';
		$msg_tourl = tsUrl('gift','notice',array('id'=>$giftid));
		aac('message')->sendmsg($msg_userid,$msg_touserid,$msg_content,$msg_tourl);

		header("Location: ".$url);
	
		break;
		
	//地图 
	case "map":
	
		$dd = isset($_GET['dd']) ? $_GET['dd'] : '中国北京';

		include template('add_map');
	
		break;

}